package forrest.triv;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

//text editing, provide instructions



public class triv extends Activity {

	int ret = 0;
	int correct=0;
	int wrong=0;
	ArrayList<ArrayList<String>> all;
	int c = 0;
	int corr;
	long oldtime;
	long newtime;
	long time;
	public static boolean back=false;
	Random rand=new Random();
	
	@Override
	protected void onResume() {
		if(back==true){
			//finish();
		}
			parse();
			super.onResume();
		}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		oldtime=System.currentTimeMillis();
		copy("questions.txt");
		parse();
	}
	
	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater men=getMenuInflater();
		men.inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==R.id.stat){
			stats();
		}
		else{
			super.finish();
		}
		return super.onOptionsItemSelected(item);
	}

	public void parse() {
		FileInputStream in = null;
		try {
			File file=new File(Environment.getExternalStorageDirectory()+"/TETA/questions.txt");
			in = new FileInputStream(file);

			if (in != null) {
				BufferedReader read = new BufferedReader(new InputStreamReader(
						in));
				String str;
				all = new ArrayList<ArrayList<String>>();
				while ((str = read.readLine()) != null) {
					ArrayList<String> strings = new ArrayList<String>();
					Log.i("", "BEF");
					String[] strs = str.split("\\|");

					for (int i = 0; i < 5; i++) {

						if (strs[i].indexOf("=") != -1) {
							ret = i;
							strings.add(strs[i].split("=")[1]);
						} else {
							strings.add(strs[i]);
						}
					}
					strings.add(Integer.toString(ret));
					all.add(strings);
				}
				createui();

			}
		} catch (FileNotFoundException e) {
			Log.i("file:", "now Where did that file go??");
		} catch (IOException e) {
			Log.i("", "it is corrupt(or this software cant read verry well");
		}
		try {
			in.close();
		} catch (IOException e) {
			Log.i("",
					"it will stay open!! quickly now \"rm -rf /\" that will stop it!");
		}
		all.remove(0);
	}

	

	public void createui() {
		corr=Integer.parseInt(all.get(c).get(5));
		TextView text = (TextView) findViewById(R.id.text);
		text.setText("");
		text.append(all.get(c).get(0));

		Button but1 = (Button) findViewById(R.id.but1);
		Button but2 = (Button) findViewById(R.id.but2);
		Button but3 = (Button) findViewById(R.id.but3);
		Button but4 = (Button) findViewById(R.id.but4);


		but1.setText(all.get(c).get(1));
		but2.setText(all.get(c).get(2));
		but3.setText(all.get(c).get(3));
		but4.setText(all.get(c).get(4));
		
		
		but1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String test = "but" + (Integer.toString(corr));
				if ("but1".equals(test)) {
					 correct();

				} else {
					wrong();
				}
				
				if (all.size()!=0) {
					c=rand.nextInt(all.size());
					createui();
					all.remove(c);
				} else {
					stats();
				}
			}
		});
		but2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String test = "but" + (Integer.toString(corr));
				if ("but2".equals(test)) {
					 correct();
				} else {
					wrong();
				}
				
				
				if (all.size()!=0) {
					c=rand.nextInt(all.size());
					createui();
					all.remove(c);
				} else {
					stats();
				}
			}
		});

		but3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String test = "but" + (Integer.toString(corr));
				if ("but3".equals(test)) {
					 correct();
				} else {
					wrong();
				}
				
				
				if (all.size()!=0) {
					c=rand.nextInt(all.size());
					createui();
					all.remove(c);
				} else {
					stats();
				}
			}
		});

		but4.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String test = "but" + (Integer.toString(corr));
				if ("but4".equals(test)) {
					 correct();
				} else {
					wrong();
				}
				
				
				
				if (all.size()!=0) {
					c=rand.nextInt(all.size());
					createui();
					all.remove(c);
				} else {
					stats();
				}
			}
		});

	}
	
	public void correct(){
		Toast correctToast=Toast.makeText(this, "correct!, one point", Toast.LENGTH_SHORT);
		correctToast.show();
		correct++;
	}
	public void wrong(){
		Toast correctToast=Toast.makeText(this, "wrong, sorry...", Toast.LENGTH_SHORT);
		correctToast.show();
		wrong++;
	}
	
	private void copy(String filename){
		InputStream inputStream = null;
		OutputStream outputStream = null;
		String myAppDirName = "/TETA/";
		File outputDir = new File(Environment.getExternalStorageDirectory() + myAppDirName);
		outputDir.mkdirs();
		File outputFile = new File(outputDir, filename);
		if(!outputFile.exists()){
			try {
				inputStream = getResources().getAssets().open(filename);
				outputStream = new FileOutputStream(outputFile);
				
				byte[] buffer = new byte[1024];
				int read;
				while ((read = inputStream.read(buffer)) != -1) {
					outputStream.write(buffer, 0, read);
				}
			} catch (IOException e) {
				Log.e("FILE_ERROR", e.getMessage());
			}
		}
	}
	
	public void stats(){
		newtime=System.currentTimeMillis();
		double score=(double)correct/((double)correct+(double)wrong);
		score=score*100;
		
		time=newtime-oldtime;
		int min=(int)(time/60000);
		int sec=(int)((time-min)/1000);
		int ms=(int)(time-min-sec)/10;
		
		String times=Integer.toString(min)+":"+Integer.toString(sec)+":"+Integer.toString(ms);
		Intent intent= new Intent(this,stats.class);
		intent.putExtra("score", score);
		intent.putExtra("time", times);
		back=true;
		this.startActivity(intent);
	}
}